package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.UserAddress;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.OrderAddressListAdapter;

public class OrderAddressLayout extends LinearLayout implements
		OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;

	private OrderAddressListAdapter listAdapter;
	private CategoriesListAsync categoriesAsync;

	// 构造函数
	public OrderAddressLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.ordershipmethod, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setText("+");
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Address");
		bodyList.setSelector(R.drawable.categories_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new OrderAddressListAdapter(context, bodyList);
		bodyList.setAdapter(listAdapter);
		
		categoriesAsync = new CategoriesListAsync(context);
		categoriesAsync.execute((Void)null);
	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			listAdapter.setSelect(arg2, arg0);
		}
	};

	class CategoriesListAsync extends
			AsyncTask<Void, Integer, ArrayList<UserAddress>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;

		public CategoriesListAsync(Context context) {
			pdialog = new ProgressDialog(context);
			pdialog.setTitle("Loading...");
			pdialog.setMessage("Please wait...");
			pdialog.setCancelable(true);
			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int i) {
					dialog.cancel();
					CategoriesListAsync.this.cancel(true);
				}
			});
			pdialog.show();
		}

		@Override
		protected ArrayList<UserAddress> doInBackground(Void... params) {
			return WebServiceClient.getUserAddress(Constants.loginResponse.getUserId(), Constants.loginResponse.getUserToken());
		}

		@Override
		protected void onPostExecute(ArrayList<UserAddress> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
			pdialog.dismiss();
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			break;
		default:
			break;
		}

	}
}
